package Controlador;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Configuration{
    
    
    public final static String ConfigurationFile = "web.xml";
    
    public static String getDataBaseURL(String path) throws IOException, Exception{
        Document Documento = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(path + ConfigurationFile));
        String URL = ((Element)Documento.getElementsByTagName("DataBase").item(0)).getElementsByTagName("URL").item(0).getTextContent();
        if(URL == null || URL.isEmpty()){throw new Exception("No encuentra etiqueta DataBase>URL");}
        return(URL);
    }
    
    public static String getDataBaseUser(String path) throws IOException, Exception{
        Document Documento = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(path + ConfigurationFile));
        String User = ((Element)Documento.getElementsByTagName("DataBase").item(0)).getElementsByTagName("User").item(0).getTextContent();
        if(User == null || User.isEmpty()){throw new Exception("No encuentra etiqueta DataBase>User");}
        return(User);
    }
    
    public static String getDataBasePassword(String path) throws IOException, Exception{
        Document Documento = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(path + ConfigurationFile));
        String Password = ((Element)Documento.getElementsByTagName("DataBase").item(0)).getElementsByTagName("Password").item(0).getTextContent();
        if(Password == null || Password.isEmpty()){throw new Exception("No encuentra etiqueta DataBase>Password");}
        return(Password);
    }
    
    public static String getDataBaseDriver(String path) throws IOException, Exception{
        Document Documento = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(path + ConfigurationFile));
        String Driver = ((Element)Documento.getElementsByTagName("DataBase").item(0)).getElementsByTagName("Driver").item(0).getTextContent();
        if(Driver == null || Driver.isEmpty()){throw new Exception("No encuentra etiqueta DataBase>Driver");}
        return(Driver);
    }
    
}